<template>
  <div class="home-purchase">
    <purchase :cneeds="needs" @handleSearch="handleSearch"></purchase>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="pageClick"
      :current-page="currentPage"
    />
  </div>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
import { selectNeedsPage } from "../api/order";
import Purchase from "@/components/Purchase.vue";
const needs = ref([]);
const total = ref(0);
const pageSize = ref(30); // 假设 API 返回 pages 字段表示总页数
const searchValue = ref("");
const currentPage = ref(1);
// const url = "/order/needs/";
const getData = async (pageNum = currentPage.value) => {
  let res = await selectNeedsPage({
    pageNum: pageNum,
    // keys: searchValue.value,
  });
   if (res.code == 0) {
    needs.value = res.data.records;
    total.value = res.data.total;
    pageSize.value = res.data.pages; // 假设 API 返回 pages 字段表示总页数
    currentPage.value = res.data.current;
  }
};

onMounted(() => {
  getData(); // 初始化数据
});

function pageClick(newPage) {
  currentPage.value = newPage;
  getData(newPage); // 获取新页的数据
}
function handleSearch(val) {
  searchValue.value = val;
  getData(); // 重置到第一页
}
</script>
 <style lang="less" scoped>
.home-purchase {
  width: 1300px;
  margin: 0 auto;
  background-color: #fff;
}
</style>